var biji = {
    getHTTPObject: function() {
        var http = false;

        if (typeof ActiveXObject != 'undefined') {
            try {
                http = new ActiveXObject('Msxml2.XMLHTTP');
            }
            catch (e) {
                try {
                    http = new ActiveXObject('Microsoft.XMLHTTP');
                }
                catch (e) {
                    http = false;
                }
            }
        } else if (window.XMLHttpRequest) {
            try {
                http = new XMLHttpRequest();
            }
            catch (e) {
                http = false;
            }
        }
		
        return http;
    },

    load: function (url, callback, method, format, opt) {
        var http = this.init();

        if (!http||!url) return;
        if (http.overrideMimeType) http.overrideMimeType('text/xml');

        if (!method) method = 'GET';
        if (!format) format = 'text';
        if (!opt) opt = {};
		
        format = format.toLowerCase();
        method = method.toUpperCase();
		
        var now = 'uid=' + new Date().getTime();
        url += (url.indexOf('?') + 1) ? '&' : '?';
        url += now;

        var parameters = null;

        if (method == 'POST') {
            var parts = url.split('\?');
            url = parts[0];
            parameters = parts[1];
        }

        http.open(method, url, true);

        if (method == 'POST') {
            http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            http.setRequestHeader('Content-length', parameters.length);
            http.setRequestHeader('Connection', 'close');
        }

        http.onreadystatechange = function () {
            if (http.readyState == 4) {
                if (http.status == 200) {
                    var result = '';

                    if (http.responseText) result = http.responseText;

                    if (format.charAt(0) == 'j') {
                        result = result.replace(/[\n\r]/g,'');
                        result = eval('('+result+')');
                    } else if (format.charAt(0) == 'x') {
                        result = http.responseXML;
                    }

                    if (callback) callback(result);
                } else {
                    if (error) error(http.status);
                }
            }
        }
		
        http.send(parameters);
    },

    bind: function(user_options) {
        var opt = {
            'url': '',
            'success': false,
            'error': false,
            'format': 'text',
            'method': 'GET'
        }

        for (var key in opt) {
            if (user_options[key]) {
                opt[key] = user_options[key];
            }
        }
		
        if (!opt.url) return;
		
        this.load(opt.url, function(data) {
            if (opt.onSuccess) opt.onSuccess(data);
        }, opt.method, opt.format, opt);
    },

    init: function() {
        return this.getHTTPObject();
    },
	
    select: function(selector, context) {
        var isObjType = function(o, type) {
            return Object.prototype.toString.call(o) === '[object '+ type +']';
        },
		
        isDesc = function (d, a) {
            return d.parentNode == a || d.tagName.toUpperCase() != 'HTML' && isDesc(d.parentNode, a);
        },
		
        s = selector, c = context, els = [];

        s = s.split(',');

        c = isObjType(c, 'String') ? $$(c) : c && c.length ? c : document;

        if (!isObjType(c, 'Array'))
            c = [c];

        for (var i in c) {
            for (var j in s) {
                var strim = s[j].replace(/\s+/g, ''),
                sp = s[j].split(' '),
                op = strim.substr(0, 1),
                name = strim.substr(1),
                tels = [],
                nextContext;

                if (sp.length > 1) {
                    nextContext = $$(sp[0], c[i]);
                    tels = $$(sp.slice(1).join(' '), nextContext);
                    els = els.concat(tels);
                } else if (op == '#') {
                    tels[0] = c[i].getElementById ? c[i].getElementById(name) : document.getElementById(name);

                    if (tels[0] && isDesc(tels[0], c[i]))
                        els.push(tels[0]);
                } else if (op == '.') {
                    var expr = new RegExp('\\b' + name + '\\b'),
                    all = c[i].getElementsByTagName('*');

                    for (var v = 0, w = all.length; v < w; v++) {
                        if (expr.test(all[v].className))
                            els.push(all[v]);
                    }
                } else {
                    tels = c[i].getElementsByTagName(strim);

                    for (var y = 0, z = tels.length; y < z; y++)
                        els.push(tels[y]);
                }
            }
        }

        result = els.length == 1 ? els[0] : els;

        result.fadeIn = function(delay, callbk, out) {
            var _this = this;
            _this.style.zoom = 1;
            _this.style.display = "block";

            for (i = 1; i <= 100; i++) {
                (function(j) {
                    setTimeout(function() {
                        if (out==true) j=100-j;
                        result.setOpacity(_this, j);
                        if (j==100&&callbk!=undefined) {
                            callbk.call(_this);
                        }
                        else if (out==true&&callbk!=undefined&&j==0) {
                            callbk.call(_this);
                        }
                    },j*delay/100);

                })(i);
            }
        },

        result.fadeOut = function(delay, callbk) {
            this.fadeIn(delay, callbk, true);
        }

        result.setOpacity = function(elm, v) {
            elm.style.opacity = v / 100;
            elm.style.MozOpacity = v / 100;
            elm.style.KhtmlOpacity = v / 100;
            elm.style.filter = " alpha(opacity ="+v+")";
        },

        result.getOpacity = function(elm) {
            var opt1 = getstyle(elm, "filter");
            var opt2 = getstyle(elm, "opacity");
            var opt3 = getstyle(elm, "MozOpacity");
            var opt4 = getstyle(elm, "KhtmlOpacity");

            if (opt1 != "none") {
                opt = opt1.replace(/[^\d]+/g, "");
            }
            else {
                opt = (opt2 != null) ? opt2 : ((opt3 != null) ? opt3 : opt4);
                opt = opt*100;
            }

            return opt;
        }

        return result;
    }
}
